package com.example.dell.spinnertext_0503guojiuqi;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.textView);
        spinner=(Spinner)findViewById(R.id.spinner);
//
//        String[] array={"北京","上海","深圳","广州","大连"};
////        ArrayAdapter<String>adapter=new ArrayAdapter<String>(
////                MainActivity.this,//上下文
////                android.R.layout.simple_spinner_item,//显示的样式
////                array);//数据源
//
//        ArrayAdapter<String>adapter=new ArrayAdapter<String>(
//                MainActivity.this,//上下文
//                R.layout.spinner_item,//单个条目显示的样式
//                array);//数据源
//        //adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
//        spinner.setAdapter(adapter);
//        spinner.setPrompt("请选择城市");//与属性配合使用：android:spinnerMode="dialog"

        //在xml里定义数据的spinner使用方式

        String[] zhuanye=getResources().getStringArray(R.array.zhuanye);
        ArrayAdapter<String> adapter2=new ArrayAdapter<String>(
                MainActivity.this,
                R.layout.spinner_item,
                zhuanye);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //String text=((TextView)View).getView().ToString();//方式1
                String text=parent.getItemAtPosition(position).toString();//方式2
                textView.setText(text);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
